package jinyilw.swing.visuals.Jcomp;

import jinyilw.common.Operator;
import jinyilw.common.math.MathTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.ResourceManager;
import jinyilw.swing.SystemTools;
import jinyilw.swing.i18n.ICSetting;
import jinyilw.swing.image.ImageTools;
import jinyilw.swing.res.LoadClass;
import jinyilw.swing.visuals.comp.ActImageComp;
import jinyilw.swing.visuals.comp.RichTextComp;

import java.awt.Image;
import java.io.File;
import java.util.ArrayList;

public class AddImageComp extends JImageComp
{
	private static final long serialVersionUID = 3256907988308668431L;
	private static final int maxS = 75;
	private static final Image addImage = ResourceManager
			.getBufferedImage(LoadClass.class, "addImage.png");
	private static final Image addImage2 = ImageTools.getContrastImage(addImage,
			1.5f);
	protected ActImageComp addComp = new ActImageComp(addImage, addImage2);
	protected RichTextComp textLabel = new RichTextComp("单击按钮或拖放文件到此处以添加图片",
			FontTools.getCustomFont(14));
	private volatile ArrayList<Operator<Object>> dropOperators;
	private boolean subAdded;
	private File imageFile;

	public AddImageComp()
	{
		addSubComps();
	}

	public void reset()
	{
		setImageFile(null);
		addSubComps();
	}

	public void addSubComps()
	{
		if (!subAdded)
		{
			subAdded = true;
			add(addComp);
			add(textLabel);
		}
	}

	public void removeSubComps()
	{
		if (subAdded)
		{
			subAdded = false;
			remove(addComp);
			remove(textLabel);
		}
	}

	public File getImageFile()
	{
		return imageFile;
	}

	public void setImageFile(File imageFile)
	{
		if (this.imageFile != imageFile)
		{
			this.imageFile = imageFile;
			setImage(ImageTools.toBufferedImage(ResourceManager.getImage(imageFile)));
		}
	}

	public void setText(String text)
	{
		textLabel.setText(text);
	}

	public void setText(ICSetting iSetting)
	{
		textLabel.setText(iSetting);
	}

	public void addOperator(Operator<Object> operator)
	{
		addComp.addClickOperator(operator);
	}

	private synchronized void createDropOperators()
	{
		if (dropOperators == null)
		{
			dropOperators = new ArrayList<>();
			SystemTools.makeDropTarget(this, o -> {
				for (Operator<Object> operator : dropOperators)
					operator.operate(o);
			});
		}
	}

	public void addDropOperator(Operator<Object> operator)
	{
		if (dropOperators == null)
			createDropOperators();
		dropOperators.add(operator);
	}

	@Override
	public void clearImage()
	{
		setImageFile(null);
	}

	protected void layoutComps(int width, int height)
	{
		if (addComp != null)
		{
			int w = width / 3, h = height / 3;
			int s = MathTools.getMin(w, h, maxS);
			int x = (width - s) / 2, y = (height - s) / 2;
			addComp.setBounds(x, y, s, s);
			textLabel.setBounds(0, height - 40, width, 20);
		}
	}

	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		boolean unResized = width == getWidth() && height == getHeight();
		super.setBounds(x, y, width, height);
		if (unResized)
			return;
		layoutComps(width, height);
	}
}
